package cn.kjcoder.redis.task;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * @author hkj
 * @email 1349091387@qq.com
 * @date 2022/1/21
 * 源码允许修改，并且本人十分乐意有 大佬 来对代码进行 随 意 调 教
 **/
@Service
@Slf4j
public class InitPrizeService {

    @Autowired
    private RedisTemplate redisTemplate;
    public static final String ZERO_PRIZE = "zero:prize";

    @PostConstruct
    public void init(){
        log.info("奖品初始化");
        Boolean hasKey = redisTemplate.hasKey(ZERO_PRIZE);
        if(!hasKey){
            List<Integer> list =  this.prize();
            list.forEach(t -> this.redisTemplate.opsForSet().add(ZERO_PRIZE,t));
        }
    }

    /**
     * 假设有10位用户参与抽奖了
     * @return
     */
    public List<Integer> prize() {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(i);
        }
        return list;
    }
}
